import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { isAuth } from '../../utils/token'

function AuthRoute ({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props => {
        if (isAuth()) {
          // 有权限
          return <Component {...props} />
        } else {
          // 没有权限
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: props.location.pathname }
              }}
            />
          )
        }
      }}
    />
  )
}

/**
function AuthRoute ({ children, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        isAuth() ? (
          children
        ) : (
          <Redirect
            to={{
              pathname: '/login',
              state: { from: props.location.pathname }
            }}
          />
        )
      }
    />
  )
}
 */

export default AuthRoute
